package com.e3.basdoc.employee.service.impl;

import java.util.Date;
import java.util.List;

import com.e3.basdoc.employee.dao.EmployeeDAO;
import com.e3.basdoc.employee.entity.EmployeeEntity;
import com.e3.basdoc.employee.model.EmployeeModel;
import com.e3.basdoc.employee.service.EmployeeCacheManager;
import com.e3.basdoc.employee.service.EmployeeService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperService;

public class EmployeeServiceImpl extends SuperService implements EmployeeService{
	
	private EmployeeDAO employeeDAO;
	
	private EmployeeDAO getEmployeeDAO(){
		if(employeeDAO == null){
			employeeDAO = DAOHelper.createDAOInstance(EmployeeDAO.class);
		}
		return employeeDAO;
	}

	public EmployeeModel newEmployee(EmployeeModel model) throws ServiceException {
		validateEmployee(model);
		checkEmployeeExisted(model);
		
		EmployeeEntity entity = employeeModelToEntity(model, null);
		getEmployeeDAO().persist(entity);
		
		model.setId(entity.getId());
		
		EmployeeCacheManager.INSTANCE.put(model);
		
		return model;
	}

	public EmployeeModel modifyEmployee(EmployeeModel model) throws ServiceException {
		validateEmployee(model);

		long id = model.getId();
		EmployeeEntity entity = getEmployeeDAO().getById(EmployeeEntity.class, id);
		if (entity == null) {
			return newEmployee(model);
		}

		String oldCode = entity.getCode();
		if (!oldCode.equals(model.getCode())) {
			checkEmployeeExisted(model);
		}

		employeeModelToEntity(model, entity);
		getEmployeeDAO().merge(entity);
		
		EmployeeCacheManager.INSTANCE.put(model);
		return model;
	}

	public void deleteEmployeeByIds(List<Long> ids) throws ServiceException {
		if(ids == null||ids.isEmpty()){
			return;
		}
		
		getEmployeeDAO().deleteEmployeeByIds(ids);
		
		EmployeeCacheManager.INSTANCE.remove(ids);
	}

	public void checkEmployeeExisted(EmployeeModel model) throws ServiceException {
		if (model == null) {
			return;
		}

		String code = model.getCode();
		String name = model.getName();
		
		StringBuffer msgs = new StringBuffer();
		long size = getEmployeeDAO().getEmployeeSizeByCode(code);
		if(size>0){
			msgs.append("编码为'" + code+"' ");
		}
		size = getEmployeeDAO().getEmployeeSizeByName(name);
		if (size>0) {
			msgs.append("名称为'" + name + "' ");
		}
		if(msgs.length()>0){
			msgs.append("已经存在，请设置新的编码和名称。");
			throw new ServiceException(msgs.toString());
		}
	}

	private void validateEmployee(EmployeeModel model) throws ServiceException{
		String code = model.getCode();
		String name = model.getName();

		if (code == null || code.trim().length() == 0 
			|| name == null || name.trim().length() == 0
			) {
			throw new ServiceException("请设置编码,名称和符号。");
		}
	}
	
	private EmployeeEntity employeeModelToEntity(EmployeeModel model,EmployeeEntity entity){
		entity = ocModelToEntity(EmployeeEntity.class, model, entity);

		entity.setCode(model.getCode());
		entity.setName(model.getName());
		entity.setBankAccount(model.getBankAccount());
		entity.setBankId(model.getBankId());
		Date date = model.getBirthday();
		if(date!=null){
			entity.setBirthday(date.getTime());
		}
		entity.setClassesId(model.getClassesId());
		entity.setDeptId(model.getDeptId());
		entity.setEducation(model.getEducation());
		entity.setEmail(model.getEmail());
		entity.setFormerName(model.getFormerName());
		entity.setGender(model.getGender());
		entity.setHomeAddress(model.getHomeAddress());
		entity.setHomePhone(model.getHomePhone());
		entity.setHomePostcode(model.getHomePostcode());
		entity.setIdcardNo(model.getIdcardNo());
		entity.setMobile(model.getMobile());
		entity.setOfficePhone(model.getOfficePhone());
		date = model.getOnboardDate();
		if(date != null){
			entity.setOnboardDate(date.getTime());
		}
		date = model.getQuitDate();
		if(date != null){
			entity.setQuitDate(date.getTime());
		}
		
		entity.setRank(model.getRank());
		entity.setRemark(model.getRemark());
		entity.setSkillLevel(model.getSkillLevel());
		entity.setStatus(model.getStatus());
		date = model.getWorkFromDate();
		if(date != null){
			entity.setWorkFromDate(date.getTime());
		}

		return entity;

	}
}
